<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Appoint;
use App\Models\Service;
use App\Models\ServiceSnapshot;
use Dingo\Api\Http\Request;

class AppointController extends BaseController
{
    public $model = Appoint::class;

    public function index(Request $request) {
        $user_id = $this->getUserId();
        $list = Appoint::where('user_id', $user_id)->orderBy("id", "desc")
            ->with('service')
            ->paginate(\request('per_page', 20));
        return $this->paginate($list);
    }

    public function info(Request $request) {
        $id = \request("id");
        if (!$id) {
            return $this->failed("参数ID不存在");
        }
        $order = Appoint::with('service')->find($id);
        if (!$order)
            return $this->failed('订单号不存在');
        if ($order->user_id != $this->getUserId())
            return $this->failed('无权查看');

        return $this->success($order);
    }

    /**
     * 保存|修改
     * @param Request $request
     * @return mixed
     * @throws \Throwable
     */
    public function store(Request $request) {
        $input = $request->input();
        $id = $input['id'] ?? 0;
        $service = null;

        if ($id) {
            $appoint = Appoint::find($id);
            if (!$id)
                return $this->failed('id对应数据不存在');

            unset($input['amount_yuan'],$input['service']);
            $appoint->update($input);
        }
        else {

            $input['category'] = -1;
            if ($input['service_id'] != -1) {
                $service = Service::find($input['service_id']);
                if (!$service)
                    return $this->failed('service_id 对应数据不存在');
                $input['category'] = $service->category ?? 0;
            }
            $user = $this->getUser();
            $input['user_id'] = $this->getUserId();

            $id = \DB::transaction(function () use($input, $user, $service){
                //生成服务快照
                if ($input['service_id'] != -1) {
                    $arr = $service->toArray();
                    unset($arr['id'], $arr['deleted_at']);
                    $shot = new ServiceSnapshot();
                    foreach ($arr as $key => $val) {
                        $shot-> {$key} = $val;
                    }
                    $shot->save();
                    $input['service_snopshot_id'] = $shot->id;

                    $service->increment('appoint_num', 1);
                }

                $input['appoint_no'] = date('Ymd') . $input['user_id'] . date('His');
                $info = Appoint::create($input);
                $user->increment('appoint_count', 1);

                return $info->id;
            });
        }

        return $this->success($id);
    }


    public function cancel(Request $request) {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $input = $request->input();
        $id = $input['id'] ?? 0;
        $user_id = $this->getUserId();
        $appoint = Appoint::find($id);
        if (!$appoint)
            return $this->failed('预约不存在');
        if ($appoint->user_id != $user_id) {
            return $this->failed('无权操作');
        }
        if (!in_array($appoint->status, [Appoint::STATUS_WAIT_EDIT_PRICE, Appoint::STATUS_WAIT_PAY])) {
            return $this->failed('当前状态不允许修改状态');
        }
        $appoint->status = Appoint::STATUS_CANCEL;
        $appoint->save();
        return $this->success(['id' => $id], '取消成功');
    }

    public function destroy(Request $request) {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $id = $request->input('id');
        $appoint = Appoint::find($id);
        if (!$appoint)
            return $this->failed('预约不存在');

        if ($appoint->user_id != $this->getUserId())
            return $this->failed('你无权删除别人的预约');

        $appoint->delete();

        return $this->success(['id' => $id]);
    }

}

